package com.anbinh.doctor.fragment.lichkham;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.SelectDayAdapter;
import com.anbinh.doctor.model.ItemMeu;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;

public class FragmentSelectDate {
	private SelectDayAdapter adapter;
	private ListView mGridView;
	private ArrayList<ItemMeu> mList;

	public FragmentSelectDate(Activity activity, int currentDate,
			final IRcallBak caBak) {
		final Dialog dialog = new Dialog(activity);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawableResource(
				android.R.color.transparent);
		dialog.setCanceledOnTouchOutside(true);

		dialog.setContentView(R.layout.fragment_selct_day);
		dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		mGridView = (ListView) dialog.findViewById(R.id.listView1);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				mList.get(position)
						.setChecked(!mList.get(position).isChecked());
				adapter.notifyDataSetChanged();
			}
		});
		mList = Utils.getMenu(activity, R.menu.day);
		for (int i = 0; i < mList.size(); i++) {
			if (i == currentDate) {
				mList.get(i).setChecked(true);
			}
		}
		adapter = new SelectDayAdapter(activity, R.layout.seprator_chuyen_khoa,
				mList, currentDate);
		mGridView.setAdapter(adapter);

		View btnBack = (View) dialog.findViewById(R.id.btnBack);
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		View lblChon = (View) dialog.findViewById(R.id.lblChon);
		lblChon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if (getCountChecked() == 0)
					return;
				Bundle bundle = new Bundle();
				bundle.putString(Utils.TIME, getShowDate());
				bundle.putString(Utils.KEY_TIME, getRepeatDate());
				caBak.onDisplay(0, bundle);
			}
		});
		dialog.show();

	}

	public int getCountChecked() {
		int count = 0;
		for (ItemMeu item : mList) {
			if (item.isChecked()) {
				count++;
			}
		}
		return count;
	}

	public String getShowDate() {
		String day = "Thứ ";
		for (ItemMeu item : mList) {
			if (item.isChecked()) {
				if (item.getRegId() != R.id.chu_nhat) {
					day = day + ",Thứ " + item.getTitle();
				} else {
					day = day + "," + item.getTitle();
				}
			}
		}
		return day.substring(5, day.length());
	}

	public String getRepeatDate() {
		String day = "";
		for (ItemMeu item : mList) {
			if (item.isChecked()) {
				if (item.getRegId() == R.id.chu_nhat) {
					day = day + "_" + 7;
				} else {
					day = day + "_" + (Integer.parseInt(item.getTitle()) - 1);
				}
			}
		}
		return day.substring(1, day.length());
	}
}
